<template>
	<view class="search-container">
		<view class="search-box">
			<view class="search-wrap">
				<uni-icons class="search" type="search"></uni-icons>
				<input type="text" placeholder="请输入商品名称" @input="input" v-model.trim="keywords">
				<uni-icons class="clear" type="clear" @click="clear" v-if="keywords !== ''"></uni-icons>
			</view>
		</view>
		<view class="search-advice" v-if="isShow">
			<view class="sugg-item" v-for="(item, i) in searchResult" :key="i" @click="gotoGoodsDetail(item)">
				<view class="goods-name">
					{{ item.goodsName }}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<view class="history-recommend" v-else>
			<view class="history-box" v-if="historyList.length > 0">
				<view class="title">
					<text>历史搜索</text>
					<uni-icons type="trash" size="18" @click="clean"></uni-icons>
				</view>
				<view class="history-list">
					<view class="tag"
					v-for="(item, index) in historyList" 
					:key="index"
					@click="gotoGoodsList(item)">
						{{ item }}
					</view>
				</view>
			</view>
			<view class="recommend-box">
				<view class="title">
					<text>热门推荐</text>
				</view>
				<view class="recommend-list">
					<view class="tag"
					v-for="(item, index) in recommendList" 
					:key="index"
					@click="gotoGoodsList(item)">
						{{ item }}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				keywords: '',
				historyList: [],
				recommendList: [],
				searchResult: [],
			}
		},
		onLoad(e) {
			if(e.keywords) {
				this.keywords = e.keywords
				this.searchGoods()
			}
			this.historyList = JSON.parse(uni.getStorageSync('keywords') || '[]')
			this.especialGoods()
		},
		computed: {
			isShow() {
				this.searchResult = this.searchResult ? this.searchResult : []
				return this.searchResult.length !== 0
			}
		},
		methods: {
			input(e) {
				console.log(e.valueOf());
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.keywords = e.detail.value
					this.searchGoods()
				}, 500)
			},
			clear() {
				this.keywords = ''
				this.searchResult = []
			},
			saveSearchHistory() {
				const set = new Set(this.historyList)
				set.delete(this.keywords)
				set.add(this.keywords)
				this.historyList = Array.from(set)
				uni.setStorageSync('keywords', JSON.stringify(this.historyList))
			},
			gotoGoodsDetail(item) {
				uni.navigateTo({
					url: `/subcontract/goods_detail/goods_detail?goodsId=${item.goodsId}`
				})
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url: `/subcontract/goods_list/goods_list?keywords=${item}`
				})
			},
			async searchGoods() {
				if(this.keywords.length === 0) {
					this.searchResult = []
					return
				}
				let params = {
					keyword: this.keywords
				}
				const { data: res } = await uni.$http.get('/goods/keyword', params)
				console.log();
				if(res.code !== 200) return uni.$showMsg()
				this.searchResult = res.data
				this.saveSearchHistory()
			},
			clean() {
				this.historyList = []
				uni.clearStorageSync('keywords', '[]')
			},
			async especialGoods() {
				const { data: res } = await uni.$http.get('/goods?promotional=1')
				// const temp = res.data.map(item => item.goodsName.split(" ", 1))
				const temp = res.data.map(item => item.keyword)
				// console.log(temp);
				temp.forEach(item => {
					if(this.recommendList.find(key => { return key == item }) === undefined) {
						// console.log(item);
						this.recommendList.push(item)
					}
				})
				console.log(this.recommendList);
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		width: 100%;
		height: 100%;
	}
	.search-container {
		width: 100%;
		height: 100%;
		background-color: #fff;
	}
	.search-box {
		top: 0;
		z-index: 999;
		position: sticky;
		height: 50px;
		display: flex;
		align-items: center;
		padding: 0 10px;
		background-color: #fff;
		.search-wrap {
			display: flex;
			width: 100%;
			background-color: #eee;
			padding: 10rpx;
			border-radius: 6rpx;

			uni-icons.search {
				margin: 0 20rpx 0 10rpx;
				font-size: 16px;
				margin-left: 5px;
			}

			uni-icons.clear {
				margin: 0 10rpx 0 20rpx;
				font-size: 16px;
				margin-left: 5px;
			}

			input {
				width: 100%;
				font-size: 14px;
			}
		}
	}
	.search-advice {
		padding: 0 5px;
		.sugg-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #f5f5f5;
			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}
	.history-recommend {
		padding: 0 5px;
		background-color: #fff;
		.title {
			height: 40px;
			display: flex;
			font-size: 14px;
			line-height: 40px;
			justify-content: space-between;
		}
		.history-box {
			.history-list {
				display: flex;
				flex-wrap: wrap;
				padding: 10rpx 0;
				// box-sizing: border-box;
				.tag {
					font-size: 12px;
					height: 18px;
					line-height: 18px;
					margin: 8rpx 0;
					margin-right: 10px;
					font-weight: 200;
					padding: 4px 7px;
					color: #000;
					border-radius: 3px;
					background-color: #f5f5f5;
				}
			}
		}
		.recommend-box {
			.recommend-list {
				display: flex;
				flex-wrap: wrap;
				padding: 10rpx 0;
				// box-sizing: border-box;
				.tag {
					font-size: 12px;
					height: 18px;
					line-height: 18px;
					margin: 8rpx 0;
					margin-right: 10px;
					font-weight: 200;
					padding: 4px 7px;
					color: #000;
					border-radius: 3px;
					background-color: #f5f5f5;
				}
			}
		}
	}
</style>
